package com.xhwl.centiotdata.integration.vehicleschedule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.integration.IntegrationCategoryEnum;
import com.xhwl.centiotdata.integration.IntegrationProductsEnum;
import com.xhwl.centiotdata.integration.vehicleschedule.sinoiov.SinoiovVehicleScheduleHandler;
import com.xhwl.centiotdata.service.integrateconfig.DeviceProjSupplierCategoryDTO;
import com.xhwl.centiotdata.service.integrateconfig.IntegrationConfigEvent;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.utils.ChkUtil;
import com.xhwl.common.vo.cent.device.DeviceProjSupplierCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * Falcon.cao @Copyright (C), 2022, 兴海物联
 * Date: 2022/10/4 13:59
 * Description: 车辆调度系统工厂
 */
@Component
public class VehicleScheduleFactory {

    private static final Map<String, VehicleScheduleSystemHandler> HANDLER_MAP = new ConcurrentHashMap<>();

    private static final Map<Integer, List<VehicleScheduleIntegrationConfig>> CONFIG_MAP = new ConcurrentHashMap<>();

    /**
     * 构建车辆调度系统处理器
     *
     * @param name 供应商编码
     */
    private VehicleScheduleSystemHandler onBuildsIntegrationHandler(String name) {
        IntegrationProductsEnum supplier = IntegrationProductsEnum.onFindingByName(name);
        if (IntegrationProductsEnum.SINOIOV_VEHICLE_SCHEDULE == supplier) {         // 中交兴路 - 车辆调度系统
            return new SinoiovVehicleScheduleHandler();
        }
        return null;
    }

    /**
     * 获取车辆调度系统处理器
     *
     * @param name 编码
     */
    public VehicleScheduleSystemHandler onFindingIntegrationHandler(String name) {
        VehicleScheduleSystemHandler handler = HANDLER_MAP.get(name);
        if (handler == null) {
            throw new BusinessException("该成品未集成车辆调度系统服务");
        }
        return handler;
    }

    /**
     * 获取车辆调度系统集成配置参数
     *
     * @param projectId 项目
     */
    public List<VehicleScheduleIntegrationConfig> onFindingIntegrationConfigs(Integer projectId) {
        List<VehicleScheduleIntegrationConfig> configs = CONFIG_MAP.get(projectId);
        if (ChkUtil.isNull(configs)) {
            throw new BusinessException("该项目未配置车辆调度系统集成服务");
        }
        return configs;
    }

    @EventListener
    public void onUpdatingIntegrations(IntegrationConfigEvent event) {
        List<DeviceProjSupplierCategoryVO> integrations = event.getDeviceProjectSupplierCategories();
        if (ChkUtil.isNull(integrations)) {
            return;
        }
        Map<String, VehicleScheduleIntegrationConfig> suppliermap = new HashMap<>();
        Map<Integer, List<VehicleScheduleIntegrationConfig>> maps = integrations.stream()
                .filter(category -> {
                    IntegrationCategoryEnum integration = IntegrationCategoryEnum.onFindingByName(category.getProdCategoryName());
                    return IntegrationCategoryEnum.VEHICLE_SCHEDULE == integration;
                })
                .map(item -> {
                    String properties = item.getProperties();
                    VehicleScheduleIntegrationConfig config = properties != null ?
                            JSON.parseObject(properties, VehicleScheduleIntegrationConfig.class) : new VehicleScheduleIntegrationConfig();
                    BeanUtils.copyProperties(item, config);

                    IntegrationProductsEnum integration = IntegrationProductsEnum.onFindingByName(item.getProductName());
                    suppliermap.put(integration.getName(), config);
                    return config;
                })
                .collect(Collectors.groupingByConcurrent(VehicleScheduleIntegrationConfig::getProjectId));

        CONFIG_MAP.clear();
        if (!maps.isEmpty()) {
            CONFIG_MAP.putAll(maps);
        }

        for (String key : HANDLER_MAP.keySet()) {
            VehicleScheduleIntegrationConfig config = suppliermap.remove(key);
            if (config == null) {
                HANDLER_MAP.remove(key);
            }
        }

        for (String key : suppliermap.keySet()) {
            VehicleScheduleSystemHandler handler = onBuildsIntegrationHandler(key);
            if (handler != null) {
                HANDLER_MAP.put(key, handler);
            }
        }
    }

}
